package com.tools.web.work.captcha;

import com.tools.common.object.Note;

import java.util.Objects;

/**
 * 滑动验证码的结果对象
 * 封装了阻塞块坐标、阻塞块图片和背景图的 Base64 字符串编码
 * */
@Note("滑动验证码的结果对象。" +
        "封装了阻塞块坐标、阻塞块图片和背景图的 Base64 字符串编码")
public class SlideCaptcha {

    @Note("背景图的 Base64 编码")
    private String canvasImage;

    @Note("阻塞块的 Base64 编码")
    private String blockImage;

    @Note("阻塞块的 X 轴坐标")
    private int blockX;

    @Note("阻塞块的 Y 轴坐标")
    private int blockY;

    @Note("允许坐标有多少像素的误差")
    private int faultTolerant;

    public SlideCaptcha(int blockX, int blockY) {
        this(blockX, blockY, 5);
    }

    public SlideCaptcha(int blockX, int blockY, int faultTolerant) {
        this.setBlockX(blockX);
        this.setBlockY(blockY);
        this.setFaultTolerant(faultTolerant);
    }

    /* ********************************************************************************************
     *
     *          Getter / Setter
     *
     * *********************************************************************************************
     * */

    public String getCanvasImage() {
        return canvasImage;
    }

    public void setCanvasImage(String canvasImage) {
        this.canvasImage = canvasImage;
    }

    public String getBlockImage() {
        return blockImage;
    }

    public void setBlockImage(String blockImage) {
        this.blockImage = blockImage;
    }

    public int getBlockX() {
        return blockX;
    }

    public void setBlockX(int blockX) {
        this.blockX = blockX;
    }

    public int getBlockY() {
        return blockY;
    }

    public void setBlockY(int blockY) {
        this.blockY = blockY;
    }

    public int getFaultTolerant() {
        return faultTolerant;
    }

    public void setFaultTolerant(int faultTolerant) {
        this.faultTolerant = Math.abs(faultTolerant);
    }

    @Override
    public String toString() {
        return "SlideCaptcha{" +
                "blockX=" + blockX +
                ", blockY=" + blockY +
                ", faultTolerant=" + faultTolerant +
                ", canvasImage=" + (canvasImage != null ? "has Value" : "null") +
                ", blockImage=" + (blockImage != null ? "has Value" : "null") +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SlideCaptcha that = (SlideCaptcha) o;
        return (blockX - that.blockX <= faultTolerant) && (blockY - that.blockY <= faultTolerant);
    }

    @Override
    public int hashCode() {
        return Objects.hash(blockX, blockY, faultTolerant);
    }
}
